<script lang="ts">
export default {
  name: "DeviceModelEditDialog"
}
</script>

<!--
 * @Descripttion: 编辑对话框
 * @version:
 * @Author: hupengwu
 * @Date: 2022-10-24 09:10:28
 * @LastEditors: hupengwu
 * @LastEditTime: 2022-10-24 09:10:28
-->
<template>
  <!-- 对话框 -->
  <el-dialog :close-on-click-modal="false" :model-value="visible" :title="dataState.title" min-width="200px"
             @close="handleCancel">

    <!-- 数据表单 -->
    <el-form ref="dataFormRef" :model="dataState.formData" :rules="dataState.rules" label-width="100px">

      <!-- 编辑框：链路名称 -->
      <el-form-item label="模型名称" prop="modelName">
        <el-input v-model="dataState.formData.modelName" :disabled="dataState.type==='update'"/>
      </el-form-item>

      <el-form-item label="提供者" prop="provider">
        <el-input v-model="dataState.formData.provider" :disabled="dataState.type==='update'"/>
      </el-form-item>

      <!-- 下拉框：链路类型 -->
      <el-form-item label="模型类型" prop="modelType">
        <el-select v-model="dataState.formData.modelType" :disabled="dataState.type==='update'" allow-create filterable placeholder="请选择">
          <el-option
            v-for="item in dataState.modelTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <!-- 下拉框：链路参数 -->
      <el-form-item label="业务参数" prop="serviceParamJson">
        <el-input v-model="dataState.formData.serviceParamJson" :autosize="{ minRows: 1, maxRows: 100 }" type="textarea"/>
      </el-form-item>

      <el-form-item label="模型参数" prop="modelSchemaJson">
        <el-input v-model="dataState.formData.modelSchemaJson" :autosize="{ minRows: 1, maxRows: 100 }" type="textarea"/>
      </el-form-item>

    </el-form>

    <!-- 脚部：确认/取消按钮-->
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="handleCancel">取 消</el-button>
      </div>
    </template>

  </el-dialog>
</template>

<script lang="ts" setup>
import {onMounted, reactive, ref, toRefs} from "vue";

import {ElForm, ElMessage} from "element-plus";
import {
  DeviceModelItem,
  CreateDeviceModelRequestVO
} from "@/api/device/model/types";

const props = defineProps(['visible']);
const emit = defineEmits(['create', 'update', 'cancel']); //注意：这个是父组件<dict-type-edit>标签上的@close
const dataFormRef = ref(ElForm); // 属性名必须和元素的ref属性值一致

/**
 * 对外声明接口：父页面可以调用它
 */
defineExpose({initEditData});

// 数据状态：定义
let dataState = reactive({
  // 类型：create/update
  type: 'create',
  // 标题
  title: '添加设备',
  // 表单数据
  formData: {
    id: undefined,
    modelType: '',
    modelName: '',
    provider: '',
    serviceParam: {},
    serviceParamJson: '{}',
    modelSchema: {},
    modelSchemaJson: '{}'
  } as CreateDeviceModelRequestVO,

  // 链路类型选项
  modelTypeOptions: [] as OptionType[],
  // 链路名称选项
  modelNameOptions: [] as OptionType[],

  // 对话框的各控件输入规则
  rules: {
    modelName: [{required: true, message: '参数不能为空', trigger: 'blur'}],
    modelType: [{required: true, message: '参数不能为空', trigger: 'blur'}],
    provider: [{required: true, message: '参数不能为空', trigger: 'blur'}],
    serviceParamJson: [{required: true, message: '参数不能为空', trigger: 'blur'}],
    modelSchemaJson: [{required: true, message: '参数不能为空', trigger: 'blur'}],
  },
});



/**
 * 取消
 */
function handleCancel() {
  //执行调用<model-edit-dialog @cancel="cancelEdit">
  emit('cancel', "子组件传给父组件的值");
}

/**
 * 确认：新增/修改实体
 */
function handleSubmit() {
  try {
    dataState.formData.serviceParam = JSON.parse(dataState.formData.serviceParamJson);
  } catch (e) {
    ElMessage.info('配置参数的格式，不是合法的JSON格式!');
    return;
  }
  try {
    dataState.formData.modelSchema = JSON.parse(dataState.formData.modelSchemaJson);
  } catch (e) {
    ElMessage.info('扩展参数的格式，不是合法的JSON格式!');
    return;
  }

  if (dataState.type === 'create') {
    // 执行调用<model-edit-dialog @create="createEdit">的create
    emit('create', dataState.formData);
  }
  if (dataState.type === 'update') {
    //执行调用<model-edit-dialog @update="updateEdit">
    emit('update', dataState.formData);
  }
}

/**
 * 初始化对话框
 * @taskParam type 对话框类型
 * @taskParam entity 实体
 */
function initEditData(type: string, entity: DeviceModelItem) {
  if (type === 'create') {
    dataState.type = type;
    dataState.title = '新增模型';

    dataState.formData.id = undefined;
    dataState.formData.modelName = '';
    dataState.formData.modelType = '';
    dataState.formData.provider = '';
    dataState.formData.serviceParam = {};
    dataState.formData.serviceParamJson = '{}';
    dataState.formData.modelSchema = {};
    dataState.formData.modelSchemaJson = '{}';

    return;
  }

  if (type === 'update') {
    dataState.type = type;
    dataState.title= '修改模型';

    dataState.formData.id = entity.id;
    dataState.formData.modelName = entity.modelName;
    dataState.formData.modelType = entity.modelType;
    dataState.formData.provider = entity.provider;
    dataState.formData.serviceParam = entity.serviceParam;
    dataState.formData.serviceParamJson = JSON.stringify(entity.serviceParam, null, 5);
    dataState.formData.modelSchema = entity.modelSchema;
    dataState.formData.modelSchemaJson = JSON.stringify(entity.modelSchema, null, 5);
    return;
  }
}

/**
 * 响应页面安装：页面的初始化工作
 */
onMounted(() => {
  dataState.modelTypeOptions.push({value: 'device', label: '设备模型'} as OptionType);
});

</script>
